-- This class defines a buffer designed to store pointers to locations in the shared memory

Library ieee;
Use ieee.std_logic_1164.all;
Use work.transmit_package.all;

Entity FramePointerBuffer Is
	Port( switchIncomingFramePointer 		: In Std_logic;
		  --switchFramePointer				: In Std_logic_vector(8 downto 0);
		  --switchCRC							: In Std_logic;
		  --switchTag							: In Std_logic_vector(31 downto 0);
		  --switchFull						: Out Std_logic; -- Make this valid everytime
		  Clock								: In Std_logic;
		  transmitInitialize				: In Std_logic;
		  transmitGetNextFramePointer		: In Std_logic;
		  --transmitEmpty						: Out Std_logic; -- Make this valid everytime
		  transmitIncoming					: Out Std_logic;
		  --transmitNextFramePointer			: Out Std_logic_vector(8 downto 0);
		  --transmitNextCRC					: Out Std_logic;
		  --transmitNextTag					: Out Std_logic_vector(31 downto 0);
		  --bufferFull						: Buffer Std_logic);
		  readRequest						: Out Std_logic;
		  writeRequest						: Out Std_logic);
End FramePointerBuffer;

Architecture Behavior Of FramePointerBuffer Is
--Constant Zeros	: Std_logic_vector(8 downto 0) := "000000000";
--Constant Zero	: Std_logic := '0';
--Constant Zeros2	: Std_logic_vector(31 downto 0) := "00000000000000000000000000000000";

-- These act as pointers to the front and back of the array, they will contain at most a single bit of 1 in a given location
--Signal pointer_to_back	: Std_logic_vector(16 downto 0);
--Signal pointer_to_back 	: Integer;
--Signal pause			: Std_logic;
--Signal MoveAll			: Std_logic;
--Signal NewPointer		: Std_logic;
--Signal moveBack			: Std_logic;

--Type vectorArray Is Array(14 downto 0) Of Std_logic_vector(8 downto 0);
--Signal addrmove			: vectorArray;

--Type vectorArray2 Is Array(14 downto 0) Of Std_logic_vector(31 downto 0);
--Signal tagmove			: vectorArray2;

--Signal CRCmove			: Std_logic_vector(15 downto 0);

--Type stateW Is (readyW, writeReady, write);
--Signal writeState		: stateW; 


Begin
	Process
	Begin
		Wait Until Clock'Event And Clock = '1';
		
		If transmitInitialize = '1' Then
			--pointer_to_back <= "00000000000000001";
			--pointer_to_back <= 0;
			--pause <= '0';
			--MoveAll <= '0';
			--NewPointer <= '0';
			--moveBack <= '0';
			--bufferFull <= '0';
			--readState <= ready;
			--writeState <= readyW;
			--transmitEmpty <= '1';
			readRequest <= '0';
			writeRequest <= '0';
			transmitIncoming <= '0';
		End If;
		
		If switchIncomingFramePointer = '1' Then
			writeRequest <= '1';
		Else
			writeRequest <= '0';
		End If;
		
		If transmitGetNextFramePointer = '1' Then
			readRequest <= '1';
			transmitIncoming <= '1';
		Else
			readRequest <= '0';
			transmitIncoming <= '0';
		End If;
			
		
		--If switchIncomingFramePointer = '1' And bufferFull = '0' Then
		--	NewPointer <= '1';
		--	moveBack <= '1';
		--Else
		--	NewPointer <= '0';
		--	moveBack <= '0';
		--End If;	
		
		--If transmitGetNextFramePointer = '1' Then
		--	pause <= '1';
		--	transmitIncoming <= '1';
		--Else
		--	pause <= '0';
		--	transmitIncoming <= '0';
		--End If;
		
		--If pause = '1' Then
		--	MoveAll <= '1';
		--	pointer_to_back <= forward(pointer_to_back);
		--Else
		--	MoveAll <= '0';
		--End If;
		
		--If moveBack = '1' Then
		--	pointer_to_back <= backward(pointer_to_back);
		--End If;
		
		
		--If pointer_to_back = "10000000000000000" Then
		--If pointer_to_back = 15 Then
		--	bufferFull <= '1';
		--Else
		--	bufferFull <= '0';
		--End If;
		
		--switchFull <= bufferFull;
		
		--If pointer_to_back = "00000000000000001" Then
		--If pointer_to_back = 0 Then
		--	transmitEmpty <= '1';
		--Else
		--	transmitEmpty <= '0';
		--End If;
				
	End Process;
	--frame0: framepointer Port Map(addrmove(0), CRCmove(0), tagmove(0), Clock, MoveAll, pointer_to_back(0) And NewPointer, switchFramePointer, switchCRC, switchTag, transmitNextFramePointer, transmitNextCRC, transmitNextTag);
	--frame_pointer_array: For i In 1 To 14 Generate
	--Begin	
	--	frame: framepointer Port Map(addrmove(i), CRCmove(i), tagmove(i), Clock, MoveAll, pointer_to_back(i) And NewPointer, switchFramePointer, switchCRC, switchTag,  addrmove(i-1), CRCmove(i-1), tagmove(i-1));
	--End Generate frame_pointer_array;
	--frame15: framepointer Port Map(Zeros, Zero, Zeros2, Clock, MoveAll, pointer_to_back(15) And NewPointer, switchFramePointer, switchCRC, switchTag, addrmove(14), CRCmove(14), tagmove(14)); 
End Behavior;